package com.gps.server;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NetUtil;
import com.gps.pojo.GPS;
import com.gps.utils.ReadWrite;
import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends Thread {

    public static void main(String[] args) {
        Server server = new Server(18888);
        server.start();
    }

    ServerSocket serverSocket = null;
    Socket socket = null;
    int port;

    public Server(int port) {
        try {
            this.port = port;
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        InputStream is = null;
        ObjectInputStream ois = null;
        while (true) {
            try {
                System.out.println("服务端启动："+ serverSocket.getInetAddress() + ":" +port);
                while (true) {
                    System.out.println("等待连接");
                    Socket socket = serverSocket.accept();
                    System.out.println("连接成功！");
                    is = socket.getInputStream();
                    ois = new ObjectInputStream(is);
                    GPS gps = (GPS)ois.readObject();// 由客户端读取过来的gps对象
                    System.out.println(gps.toString());
                    System.out.println(parseGPS(gps));
                    persistence(gps);
                }
            }  catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ois != null)
                        ois.close();
                    if (is != null)
                        is.close();
                    if (socket!= null && !socket.isClosed())
                        socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "server");
        }
    }

    // 持久化
    private static void persistence(GPS gps) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("persistence.txt", true));
            bw.write("[GPS：]" + gps.toString() + "\n[GPS解析：]\n" +parseGPS(gps));
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 解析GGA格式数据信息
    private static String parseGPS(GPS gps) {
        StringBuilder result = new StringBuilder();
        result.append("\t\t\tUTC时间：" + parseTime(gps.getTime()) + "\n");
        result.append(gps.isN() ? "\t\t\t" + gps.getLatitude() + " N\n" : "\t\t\t" + gps.getLongitude() + " S\n");
        result.append(gps.isE() ? "\t\t\t" + gps.getLongitude() + " E\n" : "\t\t\t" + gps.getLongitude() + " W\n");
        result.append("\t\t\t卫星数量：" + gps.getSatellite() + "\n");
        result.append("\t\t\tHDOP水平精度因子：" + gps.getHdop() + "\n");
        result.append("\t\t\t海拔高度：" + gps.getAltitude() + "\n");
        return new String(result);
    }

    // 解析时间
    private static String parseTime(String time) {
        return time.substring(0,2) + ":" +time.substring(2, 4) + ":" + time.substring(4);
    }

    @Test
    public void parseTimeTest() {
        System.out.println(parseTime("075503.000"));
    }

}
